﻿package kliment.controls.mediaplayer.skin {
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import kliment.controls.ControlsAbstract;
	import kliment.display.MovieClipButton;
	import kliment.events.MovieClipButtonEvent;
	import kliment.math.Calc;
	
	[Event(name = 'change', type = 'flash.events.Event')]
	
	/**
	 * ...
	 * @author Kliment
	 * 
	 * class name: kliment.controls.mediaplayer.skin.SeekerLine
	 */
	public class SeekerLine extends ControlsAbstract {
		protected var _target_mc:MovieClip;
		private var _enableDrag:Boolean = false;
		protected var _mask_mc:MovieClip;
		protected var _fill_mc:MovieClip;
		protected var _hit_mc:MovieClip;
		protected var _back_mc:MovieClip;
		protected var _seekerBtn:MovieClipButton;
		private var _position:Number = 0;
		private var _stage:Stage;
		
		public function SeekerLine(target_mc:MovieClip) {
			_target_mc = target_mc;
			_mask_mc = _target_mc['mask_mc'];
			_fill_mc = _target_mc['fill_mc'];
			_hit_mc = _target_mc['hit_mc'];
			_back_mc = _target_mc['back_mc'];
			_seekerBtn = _target_mc['seeker_mc'];
			
			_back_mc.mouseEnabled = false;
			_fill_mc.mouseEnabled = false;
			_hit_mc.mouseChildren = false;
			
			_hit_mc.addEventListener(MouseEvent.MOUSE_DOWN, _mousePressHandler);
			
			_seekerBtn.addEventListener(MovieClipButtonEvent.MOUSE_DOWN, _mousePressHandler);
			_seekerBtn.addEventListener(MovieClipButtonEvent.RELEASE, _mouseReleaseHandler);
			_seekerBtn.addEventListener(MovieClipButtonEvent.RELEASE_OUTSIDE, _mouseReleaseHandler);
			
			_target_mc.addEventListener(Event.ADDED_TO_STAGE, _stageHandler);
			if (_target_mc.stage) 
				_target_mc.dispatchEvent(new Event(Event.ADDED_TO_STAGE));
			
		}
		
		public function get position():Number { return _position }
		public function set position(value:Number):void {
			_moveTo(value, _enableDrag);
		}
		
		private function _moveTo(value:Number, stopSeeker:Boolean = false):void {
			value = Calc.inInterval(0, 1, value);
			_setPositions(value, stopSeeker);
			if (_position != value) {
				_position = value;
				if (_enableDrag)
					dispatchEvent(new Event(Event.CHANGE));
			}
		}
		
		protected function _setPositions(value:Number, stopSeeker:Boolean = false):void {
			var pos:Number = (value * _back_mc.width) + _back_mc.x;
			_fill_mc.x = pos - _back_mc.width;
			if (!stopSeeker) 
				_seekerBtn.x = pos;
		}
		
		private function _stageHandler(event:Event):void {
			switch (event.type) {
				case Event.ADDED_TO_STAGE:
					_stage = _target_mc.stage;
					_stage.addEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
					_target_mc.addEventListener(Event.REMOVED_FROM_STAGE, _stageHandler);
				break;
				
				case Event.REMOVED_FROM_STAGE:
					_stage.removeEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
					_target_mc.addEventListener(Event.ADDED_TO_STAGE, _stageHandler);
				break;
			}
		}
		
		private function _mousePressHandler(event:MouseEvent):void {
			_enableDrag = true;
			if (event.target == _hit_mc) {
				_stage.addEventListener(MouseEvent.MOUSE_UP, _mouseReleaseHandler);
				_mouseMoveHandler();
			}
		}
		
		
		private function _mouseMoveHandler(event:Event = null):void {
			if (_enableDrag) {
				_moveTo(Calc.inInterval(0, 1, (_target_mc.mouseX - _hit_mc.x) / _hit_mc.width));
			}
		}
		
		private function _mouseReleaseHandler(event:MouseEvent):void {
			_enableDrag = false;
			_stage.removeEventListener(MouseEvent.MOUSE_UP, _mouseReleaseHandler);
		}
		
		override public function set enable(value:Boolean):void {
			super.enable = value;
			_seekerBtn.visible = enable;
			_hit_mc.visible = enable;
		}
		
		public function get x():Number { return _target_mc.x; }
		public function set x(value:Number):void {
			_target_mc.x = value;
		}
		
		public function get y():Number { return _target_mc.y; }
		public function set y(value:Number):void {
			_target_mc.y = value;
		}
		
		public function get width():Number { return _back_mc.width; }
		public function set width(value:Number):void {
			_back_mc.width = value;
			_mask_mc.width = value;
			_hit_mc.width = value;
			_moveTo(_position);
		}
		
		public function remove():void {
			if (_stage) 
				_stage.removeEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
			_target_mc.removeEventListener(Event.ADDED_TO_STAGE, _stageHandler);
			_seekerBtn.removeEventListener(MovieClipButtonEvent.MOUSE_DOWN, _mousePressHandler);
			_seekerBtn.removeEventListener(MovieClipButtonEvent.RELEASE, _mouseReleaseHandler);
			_seekerBtn.removeEventListener(MovieClipButtonEvent.RELEASE_OUTSIDE, _mouseReleaseHandler);
		}
		
	}
}